<template>
  <a-modal
    :title="'采集器'+addTitle+'的远程配置'"
    :width="1200"
    :visible="visible"
    :footer="null"
    @cancel="handleCancel"
  >
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="0">
            <a-col :md="6" :sm="24">
              <a-form-item label="任务类型">
                <a-select style="width: 100%;" v-model="queryParam.taskType" allow-clear placeholder="任务类型" >
                  <a-select-option v-for="(item,index) in gatewayTaskTypeList" :key="index" :value="item.code" >{{ item.value }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons" style="padding-left: 16px;">
                <a-form-item label="选择时间">
                  <a-range-picker @change="onChange" style="width: 100%" v-model="time" />
                </a-form-item>
              </span>
            </a-col>

            <a-col :md="!advanced && 6 || 24" :sm="24">
              <span class="table-page-search-submitButtons" style="padding-left: 16px;" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" class="btn_search_reset" @click="$refs.table.refresh()"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" class="btn_search_reset" @click="() => {queryParam = {gatewayId : gatewayId.toString()};$refs.table.refresh();time = [];}"><a-icon type="reload" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="middle"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
      >
        <span slot="status" slot-scope="text">
          {{ text == 0? '禁用':'启用' }}
        </span>
        <span slot="taskType" slot-scope="text">
          {{ taskTypeFilter(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="$refs.editForm.edit(record)" class="btn_edit">详情</a>
        </span>
      </s-table>
      <edit-form ref="editForm" @ok="handleOk"/>
    </a-card>
  </a-modal>
</template>

<script>
  import { STable } from '@/components'
  import { del } from '@/api/modular/main/bizcollectorconfig/bizCollectorConfigManage'
  import { pageTask } from '@/api/modular/main/gatewaytask/gatewaytask'
  import addForm from './addForm'
  import editForm from './editForm'
  import selectForm from './selectForm'
  import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'
  import moment from 'moment'

  export default {
    components: {
      STable,
      addForm,
      editForm,
      selectForm
    },

    data () {
      return {
        time: [],
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '任务类型',
            dataIndex: 'taskType',
            width: 200,
            ellipsis: true,
            scopedSlots: { customRender: 'taskType' }
          },
          {
            title: '附加参数',
            dataIndex: 'taskParams',
            ellipsis: true
          },
          {
            title: '开始时间',
            dataIndex: 'createTime'
          },
          {
            title: '响应时间',
            dataIndex: 'issuedTime'
          },
          {
            title: '响应结果',
            dataIndex: 'response_result',
            width: 200,
            ellipsis: true
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return pageTask(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        labelCol: { span: 8 },
        wrapperCol: { span: 16 },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        addTitle: '',
        visible: false,
        gatewayId: 0,
        gatewayTaskTypeList: []
      }
    },
    created () {
      this.getGatewayTaskTypeList()
    },
    methods: {
      taskTypeFilter (record) {
        let str = ''
        this.gatewayTaskTypeList.forEach(item => {
          if (record === item.code) {
            str = item.value
          }
        })
        return str
      },
      getGatewayTaskTypeList () {
        sysDictTypeDropDown({ code: 'gateway_task_type' }).then((res) => {
          if (res.success) {
            this.gatewayTaskTypeList = res.data
          }
        })
      },
      toIndex (record) {
        this.addTitle = record.gatewayName
        this.visible = true
        this.gatewayId = record.id
        this.queryParam.gatewayId = record.id
        try {
          this.$refs.table.refresh()
        } catch (e) {
          // 首次进入界面，因表格加载顺序，会抛异常，我们不予理会
        }
      },
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      handleCancel () {
        this.queryParam = {}
        this.visible = false
      },
      onChange (date, dateString) {
        if (date.length === 0) {
          this.queryParam.startTime = ''
          this.queryParam.endTime = ''
        } else {
          this.queryParam.startTime = moment(date[0]).format('YYYY-MM-DD')
          this.queryParam.endTime = moment(date[1]).format('YYYY-MM-DD')
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }
  /deep/ .ant-row{
    margin-left: 0px !important;
  }
  .table-page-search-wrapper{
    padding-left: 10px;
  }
</style>
